/*
 *****************************************************************************
 * Copyright (C) 2005-2014 UC Mobile Limited. All Rights Reserved
 * File			: ProxyDetect.cpp
 *
 * Description	: ProxyDetect
 *
 * Author		: lixm@ucweb.com
 *
 * History		: Creation, 15-1-20, lixm@ucweb.com, Create the file
 ******************************************************************************
 **/

#include "ProxyDetect.h"
#include <Foundation/Foundation.h>


ProxyDetect* ProxyDetect::g_instance = 0;
ProxyDetect* ProxyDetect::instance()
{
    if (!g_instance)
        g_instance = new ProxyDetect();
    return g_instance;
}

std::string ProxyDetect::getProxyName()
{
    CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings();
    const CFStringRef proxyCFstr = (const CFStringRef)CFDictionaryGetValue(dicRef, (const void*)kCFNetworkProxiesHTTPProxy);
    char buffer[4096];
    memset(buffer, 0, 4096);
    if (CFStringGetCString(proxyCFstr, buffer, 4096, kCFStringEncodingUTF8))
    {
        return std::string(buffer);
    }
    return "";
}


int ProxyDetect::getProxyPort()
{
    CFDictionaryRef dicRef = CFNetworkCopySystemProxySettings();
    const CFNumberRef portCFnum = (const CFNumberRef)CFDictionaryGetValue(dicRef, (const void*)kCFNetworkProxiesHTTPPort);
    
    SInt32 port;
    if (CFNumberGetValue(portCFnum, kCFNumberSInt32Type, &port))
    {
        return port;
    }
    return -1;
}
